﻿#include "xxl.h"
#include "ui_xxl.h"

#include <QToolButton>
#include <QRandomGenerator>
#include <QDebug>
#include <QMessageBox>
#include <QAction>
#include <QGraphicsScene>

xxl::xxl(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::xxl)
{
    ui->setupUi(this);
    ui->horizontalLayout->setAlignment(Qt::AlignLeft);
    ui->widget->resize(619,639);
    ui->pushButton->hide();
    init();
    onconnect();
    updatabutstate();
}

xxl::~xxl()
{
    delete ui;
}

void xxl::init()
{
    strList.clear();
    strList<<"萝卜"<<"苹果"<<"香蕉"<<"番茄"<<"黄瓜"<<"桃子";

    for (const auto &str:qAsConst(strList))
    {
        for(int i= 0;i<6;++i)
        {
            QToolButton *but = new QToolButton(ui->widget);
            but->setFixedSize(80,80);
            but->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
            but->setIconSize(QSize(64,64));
            but->setText(str);
            if(str == "萝卜")
            {
              but->setStyleSheet("QToolButton{ background-color: #ffa502;border-radius: 4px}");
              but->setIcon(QIcon(QString("://res/lupbo.svg")));
            }
            if(str == "苹果")
            {
              but->setStyleSheet("QToolButton{ background-color: #ff4757;border-radius: 4px}");
              but->setIcon(QIcon(QString("://res/pingguo.svg")));
            }
            if(str == "香蕉")
            {
              but->setStyleSheet("QToolButton{ background-color: #f6e58d;border-radius: 4px}");
              but->setIcon(QIcon(QString("://res/xiangjiao.svg")));
            }
            if(str == "番茄")
            {
              but->setStyleSheet("QToolButton{ background-color: #ff6348;border-radius: 4px}");
              but->setIcon(QIcon(QString("://res/fanqie.svg")));
            }
            if(str == "黄瓜")
            {
              but->setStyleSheet("QToolButton{ background-color: #6ab04c; border-radius: 4px}");
              but->setIcon(QIcon(QString("://res/huanggua.svg")));
            }
            if(str == "桃子")
            {
              but->setStyleSheet("QToolButton{ background-color: #ff7675;border-radius: 4px}");
              but->setIcon(QIcon(QString("://res/taozi.svg")));
            }


            double w,h;
            double xpos = ui->widget->width()-but->width();
            double ypos = ui->widget->height()-but->height();

            while(true)
            {
                w = QRandomGenerator::global()->bounded(0.99);
                h = QRandomGenerator::global()->bounded(0.99);
                w = xpos*w;
                h = ypos*h;
                if(w < xpos && h < ypos)
                    break;
            }
            but->move(w,h);
            connect(but,&QToolButton::clicked,this,[=](){
                but->hide();
                addbutton(but->text());
                but->deleteLater();
            });

        }
    }
}

void xxl::onconnect()
{
    connect(ui->pushButton,&QPushButton::clicked,this,&xxl::returengame);
}

void xxl::addbutton(const QString& str)
{
    QToolButton* toolbut = new QToolButton(ui->widget_2);
    toolbut->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    toolbut->setText(str);
    toolbut->setFixedSize(80,80);
    toolbut->setIconSize(QSize(64,64));
    if(str == "萝卜")
    {
      toolbut->setStyleSheet("QToolButton{ background-color: #ffa502;border-radius: 4px}");
      toolbut->setIcon(QIcon(QString("://res/lupbo.svg")));
    }
    if(str == "苹果")
    {
      toolbut->setStyleSheet("QToolButton{ background-color: #ff4757;border-radius: 4px}");
      toolbut->setIcon(QIcon(QString("://res/pingguo.svg")));
    }
    if(str == "香蕉")
    {
      toolbut->setStyleSheet("QToolButton{ background-color: #f6e58d;border-radius: 4px}");
      toolbut->setIcon(QIcon(QString("://res/xiangjiao.svg")));
    }
    if(str == "番茄")
    {
      toolbut->setStyleSheet("QToolButton{ background-color: #ff6348;border-radius: 4px}");
      toolbut->setIcon(QIcon(QString("://res/fanqie.svg")));
    }
    if(str == "黄瓜")
    {
      toolbut->setStyleSheet("QToolButton{ background-color: #6ab04c; border-radius: 4px}");
      toolbut->setIcon(QIcon(QString("://res/huanggua.svg")));
    }
    if(str == "桃子")
    {
      toolbut->setStyleSheet("QToolButton{ background-color: #ff7675;border-radius: 4px}");
      toolbut->setIcon(QIcon(QString("://res/taozi.svg")));
    }
    ui->horizontalLayout->addWidget(toolbut);

    // 框里的图签
    for (const auto &str:qAsConst(strList))
    {
        int ping = 0;
        QList<QToolButton*> listbut = ui->widget_2->findChildren<QToolButton*>();
        if(listbut.count() == 8)
        {
            QMessageBox::information(this,"","方格已满，闯关失败！");
            this->close();
            return;
        }
        for(auto butchild:listbut)
        {
            if(butchild->text() == str)
            {
                ping++;
                if(ping ==3)
                {
                    treefinished(str);
                }
            }
        }
    }

    QList<QToolButton*> listmainbut = ui->widget->findChildren<QToolButton*>();
    if(listmainbut.count() == 1)
    {
        QMessageBox::information(this,"","恭喜通关！");
        this->close();
    }

    updatabutstate();
}

void xxl::updatabutstate()
{
    //允许暴露出的按钮可以点击
    QList<QToolButton*> listbut = ui->widget->findChildren<QToolButton*>();
    for(auto butchild:listbut)
    {
        butchild->setEnabled(false);
        if(isClickedable(butchild))
             butchild->setEnabled(true);
    }
}

void xxl::treefinished(const QString& str)
{
    QList<QToolButton*> list = ui->widget_2->findChildren<QToolButton*>();
    for(auto but:list)
    {
        if(but->text() == str)
        {
            but->hide();
            but->deleteLater();
        }
    }
}

void xxl::cleargame()
{
    QList<QToolButton*> listbut = ui->widget->findChildren<QToolButton*>();
    for(auto butchild:listbut)
    {
        butchild->deleteLater();
    }

    QList<QToolButton*> listbut2 = ui->widget_2->findChildren<QToolButton*>();
    for(auto butchild2:listbut2)
    {
        butchild2->deleteLater();
    }
}

void xxl::returengame()
{
    emit restart();
    this->close();
}

bool xxl::isClickedable(QToolButton *butchild)
{
    QPoint left_top(butchild->pos().rx(),butchild->pos().ry());
    QToolButton *tmp_btn = (QToolButton *) ui->widget->childAt(left_top);
    if (tmp_btn != butchild)
    {
         return false;
    }


    QPoint left_below(butchild->pos().rx(),butchild->pos().ry() + 79);
     tmp_btn = (QToolButton *) ui->widget->childAt(left_below);
    if (tmp_btn != butchild)
    {
         return false;
    }

    QPoint rigth_top(butchild->pos().rx() + 79,butchild->pos().ry());
    tmp_btn = (QToolButton *) ui->widget->childAt(rigth_top);
    if (tmp_btn != butchild)
    {
         return false;
    }

    QPoint rigth_below(butchild->pos().rx() + 79,butchild->pos().ry() + 79);
    tmp_btn = (QToolButton *) ui->widget->childAt(rigth_below);
    if (tmp_btn != butchild)
    {
         return false;
    }
    QPoint let_left(butchild->pos().rx() - 79,butchild->pos().ry() - 79);
    tmp_btn = (QToolButton *) ui->widget->childAt(rigth_below);
    if (tmp_btn != butchild)
    {
        // return false;
    }

    return true;
}
